package com.sunda.spmsweb.overseacontroller;


import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaWhsDumpDTO;
import com.sunda.spmsoversea.dto.OverseaWhsDumpQueryDTO;
import com.sunda.spmsoversea.entity.OverseaWhsDumpDtl;
import com.sunda.spmsoversea.service.IOverseaWhsDumpService;
import com.sunda.spmsuser.entity.SpmsUser;
import com.sunda.spmsuser.service.ISpmsUserService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 海外仓转储任务表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/overseaWhsDump")
@Api(tags = "海外仓转储任务", description = "海外仓转储任务")
public class OverseaWhsDumpController {

    @Autowired
    IOverseaWhsDumpService iOverseaWhsDumpService;

    @Autowired
    ISpmsUserService iSpmsUserService;

    @RequestMapping("/getOverseaWhsDumpPage")
    @ApiOperation(value = "获取转储任务表头分页", notes = "获取转储任务表头分页\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"beginDate\": \"\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"endDate\": \"\",\n" +
            "\t\"sapDeliveryNote\": \"\",\n" +
            "\t\"sapPurchaseNo\": \"\",\n" +
            "\t\"size\": 20,\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"werksDumpIn\": \"\",\n" +
            "\t\"whsDumpNo\": \"\",\n" +
            "\t\"werksDumpOut\": \"\",\n" +
            "\t\"whsLocationCodeDumpIn\": \"\",\n" +
            "\t\"whsLocationCodeDumpOut\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult getOverseaWhsDumpPage(@RequestBody OverseaWhsDumpQueryDTO overseaWhsDumpQueryDTO){
        try {
            return iOverseaWhsDumpService.getWhsDumpPage(overseaWhsDumpQueryDTO);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getWhsDumpDtls")
    @ApiOperation(value = "获取转储任务明细", notes = "获取转储任务明细\n" +
            "示例参数：\n" +
            "uuidWhsIn = cfc7014fa512597ee0530101007fd417 ", httpMethod = "GET")
    public ResponseResult getWhsDumpDtls(@RequestParam String whsDumpUuid){
        try {
            return ResponseResult.success().add("overseaWhsDumpDtls", iOverseaWhsDumpService.getWhsDumpDtls(whsDumpUuid));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/createWhsDump")
    @ApiOperation(value = "创建转储任务及明细", notes = "创建转储任务及明细\n" +
            "示例参数：\n" +
            "voucherType:凭证类型/订单类型(采购)公司内ZU01/公司间ZC01\n" +
            "purchaseGroup:采购组 240\n" +
            "purchaseOrg:采购组织 1000\n" +
            "{\n" +
            "\t\"cancelPostingDate\": \"\",\n" +
            "\t\"createDate\": \"2021-11-03\",\n" +
            "\t\"deliveryDate\": \"2022-01-20\",\n" +
            "\t\"voucherType\": \"ZU01\",\n" +
            "\t\"purchaseGroup\": \"240\",\n" +
            "\t\"purchaseOrg\": \"1000\",\n" +
            "\t\"dumpOutDate\": \"\",\n" +
            "\t\"dumpOutRemark\": \"\",\n" +
            "\t\"oaRemark\": \"Apply remarks\",\n" +
            "\t\"postingDate\": \"\",\n" +
            "\t\"purchasePostingDate\": \"\",\n" +
            "\t\"spmsStatus\": \"1\",\n" +
            "\t\"submitOaDate\": \"\",\n" +
            "\t\"werksDumpIn\": \"TF02\",\n" +
            "\t\"werksDumpOut\": \"GF02\",\n" +
            "\t\"whsDumpUuid\": \"\",\n" +
            "\t\"whsLocationCodeDumpIn\": \"1006\",\n" +
            "\t\"whsLocationCodeDumpOut\": \"2006\",\n" +
            "\t\"overseaWhsDumpDtlList\": [{\n" +
            "\t\t\"actualQtyBasicUnit\": 0,\n" +
            "\t\t\"actualQtyUnitSales\": 0,\n" +
            "\t\t\"actualRemark\": \"\",\n" +
            "\t\t\"actualStorageInfo\": \"\",\n" +
            "\t\t\"applyQtyBasicUnit\": 10,\n" +
            "\t\t\"applyQtyUnitSales\": 10,\n" +
            "\t\t\"applyRemark\": \"REMARK123\",\n" +
            "\t\t\"autoIdDumpDtl\": 0,\n" +
            "\t\t\"basicUnit\": \"PCS\",\n" +
            "\t\t\"item\": 0,\n" +
            "\t\t\"materialNo\": \"210051422\",\n" +
            "\t\t\"unitSales\": \"PCS\",\n" +
            "\t\t\"whsDumpUuid\": \"\"\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    public ResponseResult createWhsDump(@RequestBody OverseaWhsDumpDTO overseaWhsDumpDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            SpmsUser spmsUser = iSpmsUserService.getByUserId(userId);
            return iOverseaWhsDumpService.createWhsDump(overseaWhsDumpDTO, userId, spmsUser);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/closeWhsDump")
    @ApiOperation(value = "转储任务关闭", notes = "转储任务关闭\n" +
            "转储任务状态设置：海外SPMS转储单据状态:0删除；1草稿；2待审批；3SPMS审批通过(提交OA);4OA驳回(操作同1);5OA审批通过(出库中);" +
            "6出库完成;7提交SAP获取PO失败;8提交SAP获取PO;9提交SAP获取DN失败;10提交SAP获取DN成功(扣转出方库存);11转出已撤销(撤到状态5出库中，SAP撤回成功则加回库存);\n" +
            "转储任务关闭，状态变化为：1-0；4-0；5-0；\n" +
            "示例参数：uuidWhsIn = cfc7014fa512597ee0530101007fd417 ", httpMethod = "GET")
    public ResponseResult closeWhsDump(@RequestParam String whsDumpUuid, @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsDumpService.closeWhsDump(whsDumpUuid, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/addOverseaWhsDumpDtls")
    @ApiOperation(value = "新增转储明细行", notes = "新增转储明细行\n" +
            "\n", httpMethod = "POST")
    public ResponseResult addOverseaWhsDumpDtls(@RequestBody List<OverseaWhsDumpDtl> dumpDtls){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsDumpService.addOverseaWhsDumpDtls(dumpDtls, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/deleteOverseaWhsDumpDtls")
    @ApiOperation(value = "删除转储明细行", notes = "删除转储明细行\n" +
            "\n", httpMethod = "POST")
    public ResponseResult deleteOverseaWhsDumpDtls(@RequestBody List<OverseaWhsDumpDtl> dumpDtls){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsDumpService.deleteOverseaWhsDumpDtls(dumpDtls, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

//    @RequestMapping("/oaApproveWhsDump")
//    @ApiOperation(value = "OA审批通过模拟接口", notes = "OA审批通过模拟接口\n" +
//            "示例参数：oaBianhao = 1498718，oaStatus = S 审批通过，oaStatus = E 审批不通过；\n", httpMethod = "POST")
//    public ResponseResult oaApproveWhsDump(String oaBianhao, String oaStatus){
//        try {
//            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
//            return iOverseaWhsDumpService.oaApproveWhsDump(oaBianhao, oaStatus, userId);
//        }catch (Exception e){
//            e.printStackTrace();
//            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
//        }
//    }

    @RequestMapping("/updateWhsDump")
    @ApiOperation(value = "更新转储任务及明细", notes = "更新转储任务及明细\n" +
            "转储任务更新状态变化控制：1-1(保存), 1-3(提交OA), 4-4(驳回保存), 4-3(驳回重提), 5-6(出库完成)；\n" +
            "", httpMethod = "POST")
    public ResponseResult updateWhsDump(@RequestBody OverseaWhsDumpDTO overseaWhsDumpDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            SpmsUser spmsUser = iSpmsUserService.getByUserId(userId);
            return iOverseaWhsDumpService.updateWhsDump(overseaWhsDumpDTO, userId, spmsUser);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/whsDumpToSapGetPo")
    @ApiOperation(value = "转储任务获取PO", notes = "转储任务获取PO\n" +
            "提交SAP状态变化控制：6/7-8/7(获取PO)；\n" +
            "", httpMethod = "POST")
    public ResponseResult whsDumpToSapGetPo(String whsDumpUuid, String purchasePostingDate, Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsDumpService.whsDumpToSap(whsDumpUuid, purchasePostingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/whsDumpToSapGetDn")
    @ApiOperation(value = "转储任务获取DN", notes = "转储任务获取DN\n" +
            "提交SAP状态变化控制：8/9-10/9(获取DN)，获取到DN扣库存；\n" +
            "", httpMethod = "POST")
    public ResponseResult whsDumpToSapGetDn(String whsDumpUuid, String postingDate, Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsDumpService.whsDumpToSap(whsDumpUuid, postingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/cancelWhsDump")
    @ApiOperation(value = "转储任务撤销", notes = "转储任务撤销\n" +
            "海外SPMS转储单据状态:\n" +
            "     0删除；1草稿；2待审批；3SPMS审批通过(提交OA);4OA驳回(操作同1);5OA审批通过(出库中);6出库完成;7提交SAP获取PO失败;\n" +
            "     8提交SAP获取PO;9提交SAP获取DN失败;10提交SAP获取DN成功(扣转出方库存);11转出已撤销(撤到状态5出库中，SAP撤回成功则加回库存);\n" +
            "转储任务撤回状态变化及处理：\n" +
            "     2-1 更新状态; 5-1 更新状态;\n" +
            "     6-5 更新状态; 7-5 更新状态;\n" +
            "     8-5 从SAP撤回PO; 9-5 从SAP撤回PO;\n" +
            "     10-8,8-5 从SAP撤回DN、加回库存、从SAP撤回PO;\n", httpMethod = "POST")
    public ResponseResult cancelWhsDump(String whsDumpUuid, String cancelPostingDate, Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaWhsDumpService.cancelWhsDump(whsDumpUuid, cancelPostingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}
